<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>实时语音转写</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
</head>
<body>
<table align="center">
    <tr align="center">
        <td colspan="4">实时语音转写</td>
    </tr>
    <tr>
        <td colspan="4"><textarea id="text" style="width: 100%;height: 200px;"></textarea></td>
    </tr>
    <tr>
        <td width="16"><img th:src="@{/speech/img/speech.png}" width="16" height="16"></td>
        <td width="300" align="left"><span id="say">正在说话...</span></td>
        <td width="20"><input id="start" type="button" value="录音"/></td>
        <td width="20"><input id="stop" type="button" disabled value="停止"/></td>
    </tr>
</table>
<script th:src="@{/speech/js/jquery.min.js}"></script>
<script th:src="@{/speech/js/audio.websocket.js}"></script>
<script th:src="@{/speech/js/audio.recorder.js}"></script>
<script th:src="@{/speech/js/audio.speech.js}"></script>
<script th:inline="javascript">
    const config = {
        base: new String(/*[[@{/}]]*/)
        , host: window.location.host//域名 + 端口号
        , protocol: window.location.protocol//网络协议
        , hostname: window.location.hostname//域名
        , port: window.location.port//端口号
        , wsUrl: (this.protocol === 'https:' ? 'wss://' : 'ws://') + this.host + this.base + 'yitu/speech'
    };

    $('#start').on('click', function () {
        audioSpeech.start(config.wsUrl, function () {
            $('#start').attr('disabled', true);
            $('#stop').attr('disabled', false);
        }, function () {
            $('#start').attr('disabled', false);
            $('#stop').attr('disabled', true);
        }, function (data) {
            if (data.final) {
                $('#text').append(data.text + '\r\n');
            } else {
                $('#say').text(data.text);
            }
        }, function (error) {
            $('#start').attr('disabled', false);
            $('#stop').attr('disabled', true);
            alert(error);
        });
    });
    $('#stop').on('click', function () {
        audioSpeech.stop();
    });
</script>
</body>
</html>


